package leetcode.tree;

import leetcode.tree.common.TreeNode;

/**
 * @author mazouri
 * @create 2021-05-04 14:47
 * <p>
 * https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/
 */
public class _114_flatten {
    public void flatten(TreeNode root) {
        if (root == null) return;

        flatten(root.left);
        flatten(root.right);

        TreeNode rRight = root.right;
        TreeNode rLeft = root.left;

        root.left = null;
        root.right = rLeft;

        TreeNode p = root;
        while (p.right != null) {
            p = p.right;
        }
        p.right = rRight;
    }
}
